

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

class Config(object):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = 'mysql://root:mysql@localhost:3306/selfManyToMany'
    SQLALCHEMY_TRACK_MODIFICATIONS = True
    SECRET_KEY = 'asfoiajsifjafjapons'


app = Flask(__name__)
app.config.from_object(Config)

db = SQLAlchemy(app)


tb_user_follows = db.Table('tb_user_follows',
                           db.Column('follower_id',db.Integer,db.ForeignKey('info_user.id'),primary_key=True),# 粉丝id
                           db.Column('followed_id',db.Integer,db.ForeignKey('info_user.id'),primary_key=True),# 被关注人id
                           )

class User(db.Model):
    __tablename__ = "info_user"
    id = db.Column(db.Integer, primary_key=True)
    # nullable=False 表示name字段不可为null
    name = db.Column(db.String(128),unique=True,nullable=False)
    # 用户所有的粉丝,添加了单项引用followed,代表用户都关注了哪些人
    follows = db.relationship('User',
                              secondary=tb_user_follows,
                              primaryjoin=id == tb_user_follows.c.followed_id,
                              secondaryjoin=id == tb_user_follows.c.follower_id,
                              backref=db.backref('followed', lazy='dynamic'),
                              lazy='dynamic')


@app.route('/')
def index():
    return 'hello world'

if __name__ == '__main__':
    db.drop_all()
    db.create_all()

    # 数据略 TODO


    app.run()